package a07_二叉树;

/**
 * <p>
 * a29_二叉搜索树中的插入操作复习1
 * </p>
 *
 * @author flyduck
 * @since 2024/12/26
 */
public class a29_二叉搜索树中的插入操作复习1 {

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(40,
                new TreeNode(20,
                        new TreeNode(10),
                        new TreeNode(30)),
                new TreeNode(60,
                        new TreeNode(50),
                        new TreeNode(70)));
        a29_二叉搜索树中的插入操作复习1 test = new a29_二叉搜索树中的插入操作复习1();
        TreeNode treeNode1 = test.insertIntoBST(treeNode, 25);
        System.out.println();
    }

    //肯定插入到了叶子节点
    public TreeNode insertIntoBST(TreeNode currentNode, int val) {
        //遍历节点是叶子节点的子节点，创建节点返回给父节点
        //父节点.left/right=insertIntoBST(currentNode.left/right,val);

        if (currentNode == null) {
            return new TreeNode(val);
        }

        //insertIntoBST返回的是跟节点insertIntoBST(currentNode.right,val);应该返回的就是currentNode.right
        if (val > currentNode.val) {
            currentNode.right = insertIntoBST(currentNode.right,val);
        }else {
            currentNode.left = insertIntoBST(currentNode.left,val);
        }
        return currentNode;
    }
}
